﻿Imports Data

Public Class ModoPagos

    Private Property id_modoPago As Integer

    Private Sub ModoPagos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CargaLV()
    End Sub

    Private Function CargaLV() As Boolean
        Try
            Me.lvModoPago.Items.Clear()
            Using db As New SerigraphicaDBDataContext
                Dim lista As List(Of Data.Modo_Pago) = db.Modo_Pago.Select(Function(p) p).ToList
                For Each item In lista
                    Me.lvModoPago.Items.Add(New ListViewItem(New String() {item.id, item.nombre}) With {.Name = item.id})
                Next
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub IngresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles IngresarToolStripMenuItem.Click
        Me.LimpiarInterfaz()
    End Sub

    Private Function LimpiarInterfaz() As Boolean
        Try
            Me.txtNombre.Text = String.Empty
            Me.btnIngresar.Text = "Ingresar"
            Me.txtNombre.Focus()
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub ModificarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ModificarToolStripMenuItem.Click
        Try
            Me.id_modoPago = Me.lvModoPago.SelectedItems(0).Name
            Using db As New SerigraphicaDBDataContext
                Dim modoPago As Data.Modo_Pago = db.Modo_Pago.Where(Function(p) p.id = id_modoPago).Select(Function(p) p).FirstOrDefault
                Me.txtNombre.Text = modoPago.nombre
                Me.btnIngresar.Text = "Modificar"
            End Using
        Catch ex As Exception

        End Try
    End Sub

    Private Sub EliminarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EliminarToolStripMenuItem.Click
        Try
            Me.id_modoPago = Me.lvModoPago.SelectedItems(0).Name
            Using db As New SerigraphicaDBDataContext
                Dim modo_pago As Data.Modo_Pago = db.Modo_Pago.Where(Function(p) p.id = id_modoPago).Select(Function(p) p).FirstOrDefault
                db.Modo_Pago.DeleteOnSubmit(modo_pago)
                db.SubmitChanges()

            End Using
            MsgBox("Eliminado Exitosamente")
            Me.CargaLV()
            Me.LimpiarInterfaz()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnIngresar_Click(sender As System.Object, e As System.EventArgs) Handles btnIngresar.Click
        If (Me.btnIngresar.Text = "Ingresar") Then
            If (Me.Ingresar) Then
                MsgBox("Ingresado Exitosamente")
                Me.CargaLV()
                Me.LimpiarInterfaz()
            End If
        Else
            If (Me.Modificar) Then
                MsgBox("Modificado Exitosamente")
                Me.CargaLV()
                Me.LimpiarInterfaz()
            End If
        End If
    End Sub

    Private Function Ingresar() As Boolean
        Try
            Using db As New SerigraphicaDBDataContext
                Dim modo_pago As New Data.Modo_Pago
                modo_pago.nombre = txtNombre.Text
                db.Modo_Pago.InsertOnSubmit(modo_pago)
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Function Modificar() As Boolean
        Try
            Me.id_modoPago = Me.lvModoPago.SelectedItems(0).Name
            Using db As New SerigraphicaDBDataContext
                Dim modo_pago As Data.Modo_Pago = db.Modo_Pago.Where(Function(p) p.id = id_modoPago).Select(Function(p) p).FirstOrDefault
                modo_pago.nombre = txtNombre.Text
                db.SubmitChanges()
            End Using
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Private Sub btnCancelar_Click(sender As System.Object, e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub
End Class